﻿using IntelligentGateway.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Services
{
    public class SetupServices
    {
        public ResponseMessage GetAllNetworkInterface()
        {
            ResponseMessage response = new ResponseMessage();
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
            List<NetInfo> result = new List<NetInfo>();
            foreach (NetworkInterface nic in nis)
            {
                if (nic.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
                    {
                        NetInfo info = new NetInfo();
                        info.Name = nic.Name;
                        info.IPAddress = ip.Address.ToString();
                        info.MaskAddress = ip.IPv4Mask.ToString();
                        IPInterfaceProperties ipProperties = nic.GetIPProperties();
                        IPAddressCollection iPAddresses = ipProperties.DnsAddresses;
                        if (iPAddresses!=null&&iPAddresses.Count > 0) 
                        {
                            info.DNS = iPAddresses[0]==null?"": iPAddresses[0].ToString();
                        }
                        GatewayIPAddressInformationCollection gatewayIPAddressInformations = ipProperties.GatewayAddresses;
                        if (gatewayIPAddressInformations != null&&gatewayIPAddressInformations.Count>0)
                        {
                            info.GateWay = nic.GetIPProperties().GatewayAddresses[0].Address.ToString();
                        }                                             
                        result.Add(info);
                    }
                }
            }
            response.data = result;
            return response;
        }
        public ResponseMessage GetNetworkInterface(string InterfaceName) 
        {
            ResponseMessage response = new ResponseMessage();
            if(RuntimeInformation.OSDescription.ToLower().Contains("windows"))
            {
                response.errcode = 1;
                response.errmsg = "windows不支持网页获取IP";
                return response;
            }
            return response;
        }
        public ResponseMessage SetNetworkInterface(string InterfaceName)
        {
            ResponseMessage response = new ResponseMessage();
            if (RuntimeInformation.OSDescription.ToLower().Contains("windows"))
            {
                response.errcode = 1;
                response.errmsg = "windows不支持网页设置IP";
                return response;
            }
            return response;
        }
    }
}
